package com.xiaocz.hblsubstitutedriving.activities.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.common.widgets.load.LoadFactory;
import com.xiaocz.common.widgets.load.core.LoadManager;
import com.xiaocz.common.widgets.load.view.AbsView;
import com.xiaocz.common.widgets.recycler.RecyclerViewAdapter;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.activities.activity.OrderListActivity;
import com.xiaocz.hblsubstitutedriving.adapter.AnnouncementListAdapter;
import com.xiaocz.hblsubstitutedriving.adapter.GZTListAdapter;
import com.xiaocz.hblsubstitutedriving.base.BaseFragment;
import com.xiaocz.hblsubstitutedriving.model.Announcement;
import com.xiaocz.hblsubstitutedriving.model.DataBean;
import com.xiaocz.hblsubstitutedriving.model.WorkBench;
import com.xiaocz.hblsubstitutedriving.widget.EmptyView;
import com.xiaocz.hblsubstitutedriving.widget.ErrorView;
import com.xiaocz.hblsubstitutedriving.widget.LoadingView;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

public class IndexFragment extends BaseFragment {

    @Override
    protected int getLayoutSourceId() {
        return R.layout.web_view;
    }

    public static IndexFragment newInstance() {
        IndexFragment mapFragment = new IndexFragment();
        return mapFragment;
    }
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycler_view_order)
    RecyclerView mRecyclerViewOrder;
    @BindView(R.id.recycler_view)
    public RecyclerView mRecyclerView;

    /**
     * DATA
     */
    private LoadManager mLoadManager;
    private AnnouncementListAdapter announcementListAdapter;
    private GZTListAdapter gztListAdapter;

    public static void show(Context context) {
        Intent intent = new Intent(context, OrderListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initWidgets(View rootView) {
        super.initWidgets(rootView);
        mLoadManager = LoadFactory.getInstance().register(refreshLayout, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                webInfo();
            }
        });
        setRefresh();
    }

    private int page = 1;

    private void setRefresh() {
        // 下拉刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh();
                webInfo();
                webWorkbench();
            }
        });
        //加载更多
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore();
                page++;
                webInfo();
            }
        });
        webInfo();
    }

    @Override
    protected void initData() {
        super.initData();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        announcementListAdapter = new AnnouncementListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<Announcement>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<Announcement> holder, Announcement data) {
                super.onItemClick(holder, data);
            }
        });
        mRecyclerView.setAdapter(announcementListAdapter);

        mRecyclerViewOrder.setLayoutManager(new LinearLayoutManager(mContext));
        gztListAdapter = new GZTListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<DataBean>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<DataBean> holder, DataBean data) {
                super.onItemClick(holder, data);
            }
        });
        mRecyclerViewOrder.setAdapter(gztListAdapter);
        webInfo();
        webWorkbench();
    }

    /**
     * 我的工作台
     */
    private void webWorkbench() {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("date", "2019-10");
        okHttpClient.post(Common.Constant.API_IP+"api/Driver/driverWorkbench", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<WorkBench>>() {
                }.getType();
                RspModel<WorkBench> rspModel = getAnalysis( response, type, "我的工作台");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.getData() != null && !"[]".equals(rspModel.getData().toString())) {
                                gztListAdapter.replace(rspModel.getData().getData());
                        }
                        break;
                    default:
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }

    /**
     * 我的消息
     */
    private void webInfo() {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("PageIndex", page);
        asyncHttpPostFormData.addFormData("PageSize", Common.Constant.pageSize);
        okHttpClient.post(Common.Constant.API_IP+"api/Message/GetMessageList", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                mLoadManager.showStateView(ErrorView.class);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<List<Announcement>>>() {
                }.getType();
                RspModel<List<Announcement>> rspModel = getAnalysisBase(mLoadManager, response, type, "我的消息");
                if (rspModel == null) {
                    mLoadManager.showStateView(EmptyView.class);
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.getData() != null && !"[]".equals(rspModel.getData().toString())) {
                            if (page == 1) {
                                announcementListAdapter.replace(rspModel.getData());
                            } else {
                                announcementListAdapter.add(rspModel.getData());
                            }
                        } else {
                            if (page == 1) {
                                mLoadManager.showStateView(EmptyView.class);
                            } else {
                                App.getInstance().showMessage("已到底啦！");
                            }
                        }
                        break;
                    default:
                        mLoadManager.showStateView(ErrorView.class);
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }
}
